﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace FormBaseLib
{
    public class CItemKeyValue
    {
        public string msKey { get; set; }
        public string msValue { get; set; }
        public CItemKeyValue()
        {
            Clear();
        }
        public CItemKeyValue(string sKey, string sValue)
        {
            msKey = sKey;
            msValue = sValue;
        }
        public void Clear()
        {
            msKey = string.Empty;
            msValue = string.Empty;
        }

        public void CopyTo(CItemKeyValue pTo)
        {
            pTo.msKey = msKey;
            pTo.msValue = msValue;
        }
        public override string ToString()
        {
            //return base.ToString();
            return string.Format("{0},{1}", msKey, msValue);
        }
        public string[] ToStringArray()
        {
            //string[] sa = new string[] { msKey, msValue };
            return new string[] { msKey, msValue };
        }
        public void SetFrom(CItemKeyValue pFrom)
        {
            msKey = pFrom.msKey;
            msValue = pFrom.msValue;
        }
        public int SetFrom(string[] sa)
        {
            if (sa.Count() > 1)
            {
                msKey = sa[0];
                msValue = sa[1];
            }
            else if (sa.Count() > 0)
            {
                msKey = sa[0];
                msValue = string.Empty;
            }
            else
            {
                msKey = string.Empty;
                msValue = string.Empty;
                return 0;
            }

            if (string.IsNullOrEmpty(msKey))
            {
                msKey = string.Empty;
                msValue = string.Empty;
                return 0;
            }
            else if (string.IsNullOrEmpty(msValue))
            {
                msValue = string.Empty;
                return 1;
            }
            else
                return 2;
        }
        public int SetFromStringSeparator(string sInput, string sSeparator)
        {
            string[] sa;
            sa = CCommon.gSplitLeftRight(sInput, sSeparator); // only left and right string.
            return SetFrom(sa);
        }
        public int SetFromStringSeparator(string sInput, char cSeparator)
        {
            char[] caMySeparator = new char[] { cSeparator };
            string[] sa;
            sa = sInput.Split(caMySeparator, 2); // only left and right string.
            return SetFrom(sa);
        }
    }
}
